<template>
  <el-row class="login-row" justify="center" align="middle">
    <el-col :span="6">
      <el-card>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="40px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input
              v-model="ruleForm.username"
              type="text"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)"
              >登录</el-button
            >
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>
import { reactive, ref } from "vue";
import { userLogin, roleGet } from "../api/user";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
const ruleFormRef = ref();
const store = useStore();
const router = useRouter();
// 自定义表单验证函数
const validatePassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback(); //通过验证
  }
};
const validateUsername = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入账号"));
  } else {
    callback();
  }
};

//表单数据包
const ruleForm = reactive({
  username: "老板",
  password: "456",
});

// 表单验证规则
const rules = reactive({
  username: [{ validator: validateUsername, trigger: "blur" }],
  password: [{ validator: validatePassword, trigger: "blur" }],
});

//表单提交事件
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // console.log(ruleForm);
      console.log("表单验证成功", ruleForm);
      userLogin(ruleForm)
        .then(async (res) => {
          console.log(res);
          let { roleId } = res.data;
          if (!roleId) {
            throw new Error("当前账号不是管理平台账号，请联系管理员");
          }
          //根据当前用户的角色id，获取菜单权限数据
          let { data } = await roleGet(roleId);
          // console.log(data);
          res.data.roleMenu = data.roleMenu;
          store.commit("user/initUserInfoMut", res.data); //向状态机存储数据
          localStorage.setItem("vue-admin-userinfo", JSON.stringify(res.data)); //本地持久存储用户信息
          router.push("/");
          ElMessage({
            message: "登录成功",
            type: "success",
          });
        })
        .catch((err) => {
          console.log("账号或密码错误");
          // ElMessage.error("账号或密码错误");
          ElMessage({
            message: "账号或密码错误",
            type: "error",
          });
        });
    } else {
      console.log("表单验证失败");
      return false;
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped>
.login-row {
  height: 100vh;
  background-color: #d8d8d8;
}
</style>
